


#include "test_mode.h"
#include "platform.h"
#include "mYmodem.h"
#include "logfile.h"


#include "cm_sys.h"

//#include "dev_attr_lamp.h"
//#include "bsp_uart.h"
//#include "hlw8112.h"


/************************************************************************************************************* */
static uint8_t hlw_tp=0;
static uint8_t hlw_tpbuf[16], hlw_tplen;

//elec_data_t elec_data;

void hlw_tp_once(uint8_t* buf, uint8_t len)
{
    memcpy(hlw_tpbuf, buf, len);
    hlw_tplen = len;
    hlw_tp = 1;
}


/**
 * @brief 电力相关
 * 
 * @param param 
 */
void elec_task(void *param)
{
#if 0

    uart_handle_t* phuart = (uart_handle_t*)param;
    cm_log_printf(0,"uart%d_task\n",phuart->id);

    uint8_t ret = hlw_config();
    cm_log_printf(0,"hlw8112 config %s!\n", ret == 0?"ok":"failed");

    memcpy(&lamp_attr.elec, &elec_data, sizeof(elec_data_t));

    while(1)
    {
        if(hlw_tp){
            uart_send(phuart->id, hlw_tpbuf,hlw_tplen);
            osSemaphoreAcquire(phuart->sem, 10);
            phuart->rxlen = cm_uart_read( phuart->id, (void*)(phuart->rxbuf), 10, 10);
            
            uint8_t dbg_buf[64]={0},slen=0;
            if(phuart->rxlen < 10){
                for(uint8_t i = 0; i < phuart->rxlen; i++){
                    slen += sprintf((char*)&dbg_buf[slen], "%02X ", phuart->rxbuf[i]); 
                }
            }
            if(phuart->rxlen){
                printf_log(lv0, "8112 rx(%d):%s\n",phuart->rxlen, dbg_buf);
            }
            hlw_tp = 0;
            continue;
        }
        
        hlw_fresh_data(&elec_data);

        //lock
        memcpy(&lamp_attr.elec, &elec_data, sizeof(elec_data_t));
        //unlock

        osDelay(100);
    }
#endif

}

/************************************************************************************************************* */

/**
 * @brief 串口调试和ota
 * 
 * @param param 
 */
void uart_dbg_task(void *param)
{


    // uart_handle_t* phuart = (uart_handle_t*)param;
    // cm_log_printf(0,"uart%d_task\n",phuart->id);

    // const char prompt[] = "please input cmd:\n";
    // uart_send(phuart->id,(uint8_t*)prompt,strlen(prompt));

    // extern uint8_t runYmodem;



    while (1)
    {
        /*ymodem client*/
        if(runYmodem){
            int32_t ret = mYmodem_client();
            if(ret != 0)
            {
                cm_log_printf(0,"Ymodem ret = %d\n",ret);
                runYmodem = 0;
                if(ret == 1)
                {
                    cm_log_printf(0,"ota start,size = %d bytes\n", 1024 ) ;//cm_ota_get_written_size()

                 //   ret = cm_ota_upgrade();
                    logfile_write(1,"ota ret = %d\n",ret);
                }
            }
        }



        //osSemaphoreAcquire(phuart->sem, 30);


#if 0

        /*uart rx handle*/
        if (phuart->rxlen < UART_BUF_LEN){
            int tmp = cm_uart_read( phuart->id, (void*)&(phuart->rxbuf[phuart->rxlen]), 
                                    UART_BUF_LEN - phuart->rxlen, 1000);

            if(tmp == 0){//接收数据处理
                if(!phuart->rxlen)
                    continue;

                phuart->rxbuf[phuart->rxlen] = '\0';//添加终止符
                    
                cm_log_printf(0,"uart%d rxlen%d\n",phuart->id, phuart->rxlen);
                        
                if(runYmodem){
                    mYmodem_recv(phuart->rxbuf, phuart->rxlen);
                }else{
                    test_mode((char*)phuart->rxbuf, phuart->rxlen, phuart == &huart[0]?chan_uart0:chan_uart1);
                }
                phuart->rxlen = 0;
            }else{
                if(tmp > 0){
                    phuart->rxlen+=tmp;
                    continue;
                }else{
                    cm_log_printf(0,"uart%d read err %d\n",phuart->id,tmp);
                }
            }
        }else{
            phuart->rxlen = 0;
        }


#endif


    }
}

/************************************************************************************************************* */
// #include "xp3309.h"

// static uint8_t xp3309_tp=0;
// static uint8_t xp3309_tpbuf[8], xp3309_tplen;

// void xp3309_tp_once(uint8_t* buf, uint8_t len)
// {
//     memcpy(xp3309_tpbuf, buf, len);
//     xp3309_tplen = len;
//     xp3309_tp = 1;
// }

void dim_cmd_send(op_src_e src, uint8_t value)
{
    logfile_write(1,"dim src [%s], value %d", op_src_str[src], value);
    // h_xp3309.dim_w = 0xFFFF/100.0*value; 
    // h_xp3309.new_cmd = 1;
}

